package com.ctsi.system.app.rest;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.common.response.UnifyResponse;
import com.ctsi.framework.organise.security.UserInfo;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.system.app.rest.request.InsertMenuRequest;
import com.ctsi.system.domain.SysMenus;
import com.ctsi.system.service.SysMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: zhangjw
 * @Date: 2018/7/26 17:27
 * @Description: 菜单服务
 *
 */

@RestController
public class SysmenuController {
    @Autowired
    private SysMenusService sysMenusService;


    /**
     * 获取一级菜单
     * @return
     */
    @GetMapping(value = "/sysmenu/{parentId}/{menuType}",produces = "application/json")
    public UnifyResponse<List<SysMenus>> firstLevelMeuns(@PathVariable Long parentId, @PathVariable String menuType){
        //获取当前登陆用户
        UserInfo user = SecurityUtil.getUser();
        List<SysMenus> menus = this.sysMenusService.getMenusByParentId(user.getUserId(),parentId,menuType);
        return UnifyResponse.success(menus);
    }

    /**
     * 获取所有菜单，有权限控制
     * @param menuId
     * @param menuType
     * @return
     */
    @GetMapping(value = "/sysmenu/all/{menuId}/{menuType}")
    public UnifyResponse<List<SysMenus>> getAllMenus(@PathVariable Long menuId, @PathVariable String menuType){
        UserInfo user = SecurityUtil.getUser();
        List<SysMenus> menus = this.sysMenusService.getAllSysMenusWithPermission(user.getLoginId(),menuId,menuType);
        return UnifyResponse.success(menus);
    }

    /**
     * 添加菜单
     * @param menuRequest
     * @return
     */
    @PostMapping(value = "/sysmenu/add")
    public UnifyResponse<List<SysMenus>> add(@RequestBody InsertMenuRequest menuRequest){
        UserInfo user = SecurityUtil.getUser();
        SysMenus menus = menuRequest.toSysMenus();
        this.sysMenusService.insertSysMenus(menus);
        Long parentId = menuRequest.getParentId();
        //获取父节点菜单，用于更新前台UI
        List<SysMenus> sysMenus =  this.sysMenusService.getAllMenusByMenuType(user.getUserId(),parentId,menus.getMenuType());
        return UnifyResponse.success(sysMenus);
    }
    /*
     * 编辑菜单，根据id查询菜单详情
     * */
    @PostMapping(value = "/sysmenu/detail",produces = "application/json;charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<SysMenus> selectDetail(@RequestParam(value = "id") String id){
        if (UtilValidate.isEmpty(id)){
            return UnifiedResponse.ok(null);
        }else {
            SysMenus sysMenusById = sysMenusService.selectSysMenusById(id);
            return UnifiedResponse.ok(sysMenusById);
        }
    }
    /*
     * 修改菜单
     * */
    @PostMapping(value = "/sysmenu/edit",consumes = "application/json",produces = "application/json;charset=UTF-8")
   @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<Integer> edit(@RequestBody SysMenus sysMenus){
        int i = sysMenusService.updateSysMenusByPKSelective(sysMenus);
        return UnifiedResponse.ok(i);
    }
    /*
     * 删除菜单
    * */
    @PostMapping(value = "/sysmenu/delete",produces = "application/json;charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<Integer> deleteSysMenu(@RequestParam(value = "id") String id){
        int i = sysMenusService.removeSysMenus(id);
        return UnifiedResponse.ok(i);
    }

}
